//
//  YLContinuationRecordViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/31.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLContinuationRecordViewModel.h"

#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLContinuationRecordModel.h"
@implementation YLContinuationRecordViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
               
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetContinuedContractCode", YLURL];
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
                
                NSLog(@"%@", dateStr);
                
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
                params[@"MonthCount"] = @(12);
                params[@"TimeIndex"] = dateStr;
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        YLContinuationRecordItem *item = [YLContinuationRecordItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        weakSelf.group = item.item;
                        
                        [subscriber sendNext:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    [subscriber sendCompleted];
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
            
        }];
        
        
    }
   return _requestCommand;
}



- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}

- (NSArray *)group
{
    if (!_group) {
        _group = [NSArray array];
    }
    return _group;
}

@end
